//
//  NetworkManager.swift
//  Weibo(Swift)
//
//  Created by 阳 on 16/7/11.
//  Copyright © 2016年 阳. All rights reserved.
//

import UIKit
import AFNetworking
//let appKey = "2705052576"
//let appSecrect = "b96bc04be7471238f0b4f5f83c169514"
//let redirectURL = "https://www.baidu.com"
//
//let userName = "18703866003"
//let userPassword = "5221635"
let appKey = "585609419"
let appSecrect = "d819c7b72a71073330fe496b1d87f0ac"
let redirectURL = "https://www.baidu.com"

let userName = "dxusdcom@163.com"
let userPassword = "516516"

enum RequestType : String
{
    case GET = "GET"
    case POST = "POST"
}
class NetworkManager: AFHTTPSessionManager {

    /// 网络管理单例
    static let sharedManager : NetworkManager = {
        let sharedManager = NetworkManager()
        sharedManager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return sharedManager
    }()
    
    /**
     网络请求方法
     
     - parameter urlString:  url
     - parameter type:       请求类型
     - parameter parameters: 附加参数
     - parameter callBack:   回调闭包
     */
    func request(urlString: String, type: RequestType, parameters: [String : AnyObject]?, callBack: (([String : AnyObject]?) -> Void))
    {
        let successed = {
            (task : NSURLSessionDataTask,response : AnyObject?) in
            if let response = response as? [String : AnyObject]
            {
                callBack(response)
            }
            else
            {
                callBack(nil)
            }
        }
        let failure = {
            (task : NSURLSessionDataTask?,error : NSError) in
            print(error)
            callBack(nil)
        }
        if type == .GET
        {
            self.GET(urlString, parameters: parameters, progress: nil, success: successed, failure: failure)
        }
        if type == .POST
        {
            self.POST(urlString, parameters: parameters, progress: nil, success: successed, failure: failure)
        }
        
    }
    
    
}
